import numpy as np

def distance(box_1, box_2):
    x1, y1, x2, y2 = box_1
    x3, y3, x4, y4 = box_2
    dis = abs(x3 - x1) + abs(y3 - y1) + abs(x4 - x2) + abs(y4 - y2)
    dis_2 = abs(x3 - x1) + abs(y3 - y1)
    dis_3 = abs(x4 - x2) + abs(y4 - y2)
    return dis + min(dis_2, dis_3)


def compute_iou(rec1, rec2):
    """
    computing IoU
    :param rec1: (y0, x0, y1, x1), which reflects
            (top, left, bottom, right)
    :param rec2: (y0, x0, y1, x1)
    :return: scala value of IoU
    """
    # computing area of each rectangles
    S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1])
    S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1])

    # computing the sum_area
    sum_area = S_rec1 + S_rec2

    # find the each edge of intersect rectangle
    left_line = max(rec1[1], rec2[1])
    right_line = min(rec1[3], rec2[3])
    top_line = max(rec1[0], rec2[0])
    bottom_line = min(rec1[2], rec2[2])

    # judge if there is an intersect
    if left_line >= right_line or top_line >= bottom_line:
        return 0.0

    intersect = (right_line - left_line) * (bottom_line - top_line)
    return (intersect / (sum_area - intersect)) * 1.0

def match_result(dt_boxes, cell_bboxes, min_iou=0.1**8):
    matched = {}
    for i, gt_box in enumerate(dt_boxes):
        distances = []
        for j, pred_box in enumerate(cell_bboxes):
            if len(pred_box) == 8:
                pred_box = [
                    np.min(pred_box[0::2]),
                    np.min(pred_box[1::2]),
                    np.max(pred_box[0::2]),
                    np.max(pred_box[1::2]),
                ]
            distances.append(
                (distance(gt_box, pred_box), 1.0 - compute_iou(gt_box, pred_box))
            )  # compute iou and l1 distance
        sorted_distances = distances.copy()
        # select det box by iou and l1 distance
        sorted_distances = sorted(
            sorted_distances, key=lambda item: (item[1], item[0])
        )
        # must > min_iou
        if sorted_distances[0][1] >= 1 - min_iou:
            continue

        if distances.index(sorted_distances[0]) not in matched:
            matched[distances.index(sorted_distances[0])] = [i]
        else:
            matched[distances.index(sorted_distances[0])].append(i)
    return matched

if __name__ == "__main__":
    ...